package fun.chutianshu.eescore_07_aop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fun.chutianshu.eescore_07_aop.entity.UserInfo;
import fun.chutianshu.eescore_07_aop.mappers.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/login")
public class LoginController {

    @Autowired
    UserInfoMapper userInfoMapper;

    @PostMapping(value = "/")
    @CrossOrigin
    public JSONObject index(@RequestBody JSONObject request) {
        System.out.println(request);
        String user_name = request.getString("user_name");
        String user_pwd = request.getString("user_pwd");
        System.out.println("输入的用户名： >>>>>>>>>>>>>>>>>>>>>>" + user_name);
        System.out.println("输入的密码：   >>>>>>>>>>>>>>>>>>>>>>" + user_pwd);

        UserInfo userInfo = userInfoMapper.findByName(user_name);

        System.out.println(userInfo.toString());

        if (userInfo == null || !user_pwd.equals(userInfo.getUser_pwd())) {
            return JSON.parseObject("{\"message\":\"用户名或密码错误！\",\"code\":403}");
        }
        else {
            System.out.println("用户验证通过，"+ userInfo.toString());
        }
        return JSON.parseObject("{\"message\":\"SUCCESS\",\"code\":200,\"token\":\"token!123\"}");
    }

}
